//
// Created by 32827 on 2023/11/21.
//
#include "yzf_i2c.h"
YZF_RESULT yzf_i2c_open(YZF_I2C_POINTER i2c){
    switch ((uint32_t)i2c) {
        case (uint32_t)I2C1:
            RCC->APB1ENR |=RCC_APB1ENR_I2C1EN;
            break;
        case (uint32_t)I2C2:
            RCC->APB1ENR |=RCC_APB1ENR_I2C2EN;
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_i2c_source_clock_freq(YZF_I2C_POINTER i2c,uint8_t freq){
    i2c->CR2 &= ~(0b111111<<I2C_CR2_FREQ_Pos);
    i2c->CR2 |= freq<<I2C_CR2_FREQ_Pos;
    return YZF_OK;
}
//设置起始位
YZF_RESULT yzf_i2c_start(YZF_I2C_POINTER i2c){
    i2c->CR1 |= 0b1 << I2C_CR1_START_Pos;
    return YZF_OK;
}
YZF_RESULT yzf_i2c_write_data(YZF_I2C_POINTER i2c,YZF_I2C_DATA data){
    //i2c->DR &= ~(0xFF << I2C_DR_DR_Pos);
    i2c->DR = data << I2C_DR_DR_Pos;
    return YZF_OK;
}
YZF_I2C_DATA yzf_i2c_read_data(YZF_I2C_POINTER i2c){
    return i2c->DR & (0xFF << I2C_DR_DR_Pos);
}
YZF_RESULT yzf_i2c_set_address_7(YZF_I2C_POINTER i2c,uint8_t address){
    i2c->OAR1 &= ~(0b1 << I2C_OAR1_ADDMODE_Pos);
    i2c->OAR1 |= 0b0 << I2C_OAR1_ADDMODE_Pos;
    i2c->OAR1 &= ~(0b1111111 << I2C_OAR1_ADD1_Pos);
    i2c->OAR1 |= address << I2C_OAR1_ADD1_Pos;
    return YZF_OK;
}
//设置10位地址
YZF_RESULT yzf_i2c_set_address_10(YZF_I2C_POINTER i2c,uint16_t address){
    i2c->OAR1 &= ~(0b1 << I2C_OAR1_ADDMODE_Pos);
    i2c->OAR1 |= 0b1 << I2C_OAR1_ADDMODE_Pos;
    i2c->OAR1 &= ~(0b1111111111 << I2C_OAR1_ADD0_Pos);
    i2c->OAR1 |= address << I2C_OAR1_ADD0_Pos;
    return YZF_OK;
}
//软件复位，BUSY位为1，总线上没有停止条件 逻辑死循环时
YZF_RESULT yzf_i2c_soft_reset(YZF_I2C_POINTER i2c){
    i2c->CR1 |= 0b1 << I2C_CR1_SWRST_Pos;
    return YZF_OK;
}
//应答使能
//软件复位，BUSY位为1，总线上没有停止条件 逻辑死循环时
YZF_RESULT yzf_i2c_soft_reset_clear(YZF_I2C_POINTER i2c){
    i2c->CR1 &= ~(0b1 << I2C_CR1_SWRST_Pos);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_ack_enable(YZF_I2C_POINTER i2c){
    i2c->CR1 |= 0b1 << I2C_CR1_ACK_Pos;
    return YZF_OK;
}
YZF_RESULT yzf_i2c_ack_disable(YZF_I2C_POINTER i2c){
    i2c->CR1 &= ~(0b1 << I2C_CR1_ACK_Pos);
    return YZF_OK;
}
//停止条件产生 记得取消ack
YZF_RESULT yzf_i2c_stop(YZF_I2C_POINTER i2c){
    i2c->CR1  |= 0b1 << I2C_CR1_STOP_Pos;
    return YZF_OK;
}
//禁止时钟延长（从模式）
YZF_RESULT yzf_i2c_disable_clock_stretching(YZF_I2C_POINTER i2c){
    i2c->CR1 &= ~(0b1 << I2C_CR1_NOSTRETCH_Pos);
    return YZF_OK;
}
//广播呼叫使能 响应地址00h
YZF_RESULT yzf_i2c_call_enable(YZF_I2C_POINTER i2c){
    i2c->CR1 |= 0b1 << I2C_CR1_ENGC_Pos;
    return YZF_OK;
}
YZF_RESULT yzf_i2c_call_disable(YZF_I2C_POINTER i2c){
    i2c->CR1 &= ~(0b1 << I2C_CR1_ENGC_Pos);
    return YZF_OK;
}
//PEC使能
YZF_RESULT yzf_i2c_pec_enable(YZF_I2C_POINTER i2c){
    i2c->CR1 |= 0b1 << I2C_CR1_ENPEC_Pos;
    return YZF_OK;
}
YZF_RESULT yzf_i2c_pec_disable(YZF_I2C_POINTER i2c){
    i2c->CR1 &= ~(0b1 << I2C_CR1_ENPEC_Pos);
    return YZF_OK;
}
//APR使能
YZF_RESULT yzf_i2c_apr_enable(YZF_I2C_POINTER i2c){
    i2c->CR1 |= 0b1 << I2C_CR1_ENARP_Pos;
    return YZF_OK;
}
YZF_RESULT yzf_i2c_apr_disable(YZF_I2C_POINTER i2c){
    i2c->CR1 &= ~(0b1 << I2C_CR1_ENARP_Pos);
    return YZF_OK;
}
//i2c使能
YZF_RESULT yzf_i2c_enable(YZF_I2C_POINTER i2c){
    i2c->CR1 |= 0b1 << I2C_CR1_PE_Pos;
    return YZF_OK;
}
YZF_RESULT yzf_i2c_disable(YZF_I2C_POINTER i2c){
    i2c->CR1 &= ~(0b1 << I2C_CR1_PE_Pos);
    return YZF_OK;
}
//DMA最后一次传输
YZF_RESULT yzf_i2c_dma_last_transfer(YZF_I2C_POINTER i2c){
    i2c->CR2 |= 0b1 << I2C_CR2_LAST_Pos;
    return YZF_OK;
}
//DMA请求使能
YZF_RESULT yzf_i2c_dma_enable(YZF_I2C_POINTER i2c){
    i2c->CR2 |= 0b1 << I2C_CR2_DMAEN_Pos;
    return YZF_OK;
}
YZF_RESULT yzf_i2c_dma_disable(YZF_I2C_POINTER i2c){
    i2c->CR2 &= ~(0b1 << I2C_CR2_DMAEN_Pos);
    return YZF_OK;
}
//缓冲器中断使能，控制TxE RxNE
YZF_RESULT yzf_i2c_buffer_interrupt_enable(YZF_I2C_POINTER i2c){
    i2c->CR2 |= 0b1 << I2C_CR2_ITBUFEN_Pos;
    return YZF_OK;
}
YZF_RESULT yzf_i2c_buffer_interrupt_disable(YZF_I2C_POINTER i2c){
    i2c->CR2 &= ~(0b1 << I2C_CR2_ITBUFEN_Pos);
    return YZF_OK;
}
//事件中断使能 传输非错误事件中断，都要他来设置
YZF_RESULT yzf_i2c_event_interrupt_enable(YZF_I2C_POINTER i2c){
    i2c->CR2 |= 0b1 << I2C_CR2_ITEVTEN_Pos;
    return YZF_OK;
}
YZF_RESULT yzf_i2c_event_interrupt_disable(YZF_I2C_POINTER i2c){
    i2c->CR2 &= ~(0b1 << I2C_CR2_ITEVTEN_Pos);
    return YZF_OK;
}
//双地址使能 只限7为地址模式
YZF_RESULT yzf_i2c_address2_enable(YZF_I2C_POINTER i2c){
    i2c->OAR2 |= 0b1 << I2C_OAR2_ENDUAL_Pos;
    return YZF_OK;
}
YZF_RESULT yzf_i2c_address2_disable(YZF_I2C_POINTER i2c){
    i2c->OAR2 &= ~(0b1 << I2C_OAR2_ENDUAL_Pos);
    return YZF_OK;
}
//设置第二个地址
YZF_RESULT yzf_i2c_set_address2(YZF_I2C_POINTER i2c,uint8_t address){
    i2c->OAR2 &= ~(0xFF << I2C_OAR2_ADD2_Pos);
    i2c->OAR2 |= address << I2C_OAR2_ADD2_Pos;
    return YZF_OK;
}
//FLAG Timeout
YZF_BOOL yzf_i2c_flag_timeout(YZF_I2C_POINTER i2c){
    return (i2c->SR1 & (0b1 << I2C_SR1_TIMEOUT_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//FLAG PECERR PEC错误
YZF_BOOL yzf_i2c_flag_PECERR(YZF_I2C_POINTER i2c){
    return (i2c->SR1 & (0b1 << I2C_SR1_PECERR_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//FLAG OVR 过载或者欠载
YZF_BOOL yzf_i2c_flag_OVR(YZF_I2C_POINTER i2c){
    return (i2c->SR1 & (0b1 << I2C_SR1_OVR_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//FLAG AF 应答失败
YZF_BOOL yzf_i2c_flag_AF(YZF_I2C_POINTER i2c){
    return (i2c->SR1 & (0b1 << I2C_SR1_AF_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//FLAG ARLO 应答丢失
YZF_BOOL yzf_i2c_flag_ARLO(YZF_I2C_POINTER i2c){
    return (i2c->SR1 & (0b1 << I2C_SR1_ARLO_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//FLAG BERR 总线错误
YZF_BOOL yzf_i2c_flag_BERR(YZF_I2C_POINTER i2c){
    return (i2c->SR1 & (0b1 << I2C_SR1_BERR_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//FLAG TC 传输完成
YZF_BOOL yzf_i2c_flag_TxE(YZF_I2C_POINTER i2c){
    return (i2c->SR1 & (0b1 << I2C_SR1_TXE_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//FLAG TB 传输完成
YZF_BOOL yzf_i2c_flag_RxNE(YZF_I2C_POINTER i2c){
    return (i2c->SR1 & (0b1 << I2C_SR1_RXNE_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//停止条件检测为
YZF_BOOL yzf_i2c_flag_STOPF(YZF_I2C_POINTER i2c){
    return (i2c->SR1 & (0b1 << I2C_SR1_STOPF_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//ADD10位头序列已发送
YZF_BOOL yzf_i2c_flag_add10(YZF_I2C_POINTER i2c){
    return (i2c->SR1 & (0b1 << I2C_SR1_ADD10_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//字发送结束
YZF_BOOL yzf_i2c_flag_BTF(YZF_I2C_POINTER i2c){
    return (i2c->SR1 & (0b1 << I2C_SR1_BTF_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//地址已发送/地址匹配
YZF_BOOL yzf_i2c_flag_ADDR(YZF_I2C_POINTER i2c){
    return (i2c->SR1 & (0b1 << I2C_SR1_ADDR_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//起始位
YZF_BOOL yzf_i2c_flag_SB(YZF_I2C_POINTER i2c){
    return (i2c->SR1 & (0b1 << I2C_SR1_SB_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//PEC出错时与上PEC对应
YZF_I2C_PEC_DATA yzf_i2c_get_pec(YZF_I2C_POINTER i2c){
    return i2c->SR2 & (0xFF << I2C_SR2_PEC_Pos);
}
//双标志
YZF_BOOL yzf_i2c_flag_DUALF(YZF_I2C_POINTER i2c){
    return (i2c->SR2 & (0b1 << I2C_SR2_DUALF_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//广播呼叫地址
YZF_BOOL yzf_i2c_flag_GENCALL(YZF_I2C_POINTER i2c){
    return (i2c->SR2 & (0b1 << I2C_SR2_GENCALL_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//发送/接收标志位 False为接收到数据
YZF_BOOL yzf_i2c_flag_TRA(YZF_I2C_POINTER i2c){
    return (i2c->SR2 & (0b1 << I2C_SR2_TRA_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//总线繁忙
YZF_BOOL yzf_i2c_flag_BUSY(YZF_I2C_POINTER i2c){
    return (i2c->SR2 & (0b1 << I2C_SR2_BUSY_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//主从模式 0为从
YZF_BOOL yzf_i2c_flag_MSL(YZF_I2C_POINTER i2c){
    return  (i2c->SR2 & (0b1 << I2C_SR2_MSL_Pos)) == 0 ? YZF_FALSE : YZF_TRUE;
}
//高速模式
YZF_RESULT yzf_i2c_set_high_mode(YZF_I2C_POINTER i2c){
    i2c->CCR |= 0b1 << I2C_CCR_FS_Pos;
    return YZF_OK;
}
//标准模式
YZF_RESULT yzf_i2c_set_normal_mode(YZF_I2C_POINTER i2c){
    i2c->CCR &= ~(0b1 << I2C_CCR_FS_Pos);
    return YZF_OK;
}
//快速模式占空比
YZF_RESULT yzf_i2c_duty_cycle_2(YZF_I2C_POINTER i2c){
    i2c->CCR &= ~(0b1 << I2C_CCR_DUTY_Pos);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_duty_cycle_16_9(YZF_I2C_POINTER i2c){
    i2c->CCR |= 0b1 << I2C_CCR_DUTY_Pos;
    return YZF_OK;
}
//分频系数
YZF_RESULT yzf_i2c_set_CCR(YZF_I2C_POINTER i2c,uint16_t ccr){
    i2c->CCR = (ccr & 0xFFF) | (i2c->CCR & ~0xFFF);
    return YZF_OK;
}
//最大上升时间
YZF_RESULT yzf_i2c_trise(YZF_I2C_POINTER i2c,uint16_t trise){
    i2c->TRISE = (trise & 0x3F) | (i2c->TRISE & ~0x3F);
    return YZF_OK;
}
